package cst.a931xmc.yuejian20.Model;

import cst.a931xmc.yuejian20.Bean.LoginBean;
import cst.a931xmc.yuejian20.Interface.LoginIface;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Service.LoginService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginModel<T> extends ApiModel implements LoginIface {
    private Retrofit retrofit;
    private LoginService service;

    public LoginModel() {
        retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(LoginService.class);
    }

    @Override
    public void login(String user_tel, String user_password, final Listener l) {
        Call<LoginBean> call = service.login(user_tel,user_password);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    l.onResponse(response.body());
                }
                else
                    l.onFail("用户不存在");
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

}
